package pers.xhh.shopapi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.xhh.shopapi.common.Mi;
import pers.xhh.shopapi.common.ResultCode;
import pers.xhh.shopapi.dao.MiGoodsAttrDao;
import pers.xhh.shopapi.entity.MiAttributeEntity;
import pers.xhh.shopapi.entity.MiGoodsAttrEntity;
import pers.xhh.shopapi.service.MiAttributeService;
import pers.xhh.shopapi.service.MiGoodsAttrService;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wei-xhh
 * @date 2020-11-22
 */
@Service
public class MiGoodsAttrServiceImpl extends ServiceImpl<MiGoodsAttrDao, MiGoodsAttrEntity> implements MiGoodsAttrService {

    @Autowired
    MiAttributeService miAttributeService;

    @Override
    public Mi getAttrById(Integer id) {
        if(id == null || id <= 0){
            return Mi.error(ResultCode.ERROR308.getMessage(), ResultCode.ERROR308.getStatus());
        }
        MiGoodsAttrEntity byId = this.getById(id);
        if(byId != null){
            Integer attrId = byId.getAttrId();
            MiAttributeEntity miAttributeEntity = miAttributeService.getById(attrId);
            String attrName = miAttributeEntity.getAttrName();

            Map<String, Object> map = new HashMap<>();
            map.put("id", id);
            map.put("attrName", attrName);
            map.put("attrValue", byId.getAttrValue());
            return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), map);
        } else {
            return Mi.error(ResultCode.ERROR603.getMessage(), ResultCode.ERROR603.getStatus());
        }
    }

    @Override
    public Mi updateAttrById(Integer id, String attrValue) {
        if(id == null || id <= 0){
            return Mi.error(ResultCode.ERROR308.getMessage(), ResultCode.ERROR308.getStatus());
        }
        MiGoodsAttrEntity miGoodsAttrEntity = new MiGoodsAttrEntity();
        miGoodsAttrEntity.setId(id);
        miGoodsAttrEntity.setAttrValue(attrValue);
        boolean b = this.updateById(miGoodsAttrEntity);
        if(b){
            return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), "更新成功！");
        }
        return Mi.error(ResultCode.ERROR603.getMessage(), ResultCode.ERROR603.getStatus());
    }

    @Override
    public Mi deleteAttr(Integer id) {
        boolean b = this.removeById(id);
        if(b){
            return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), "删除成功");
        }
        return Mi.error(ResultCode.ERROR603.getMessage(), ResultCode.ERROR603.getStatus());
    }
}
